﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.IO;

namespace PictureTool
{
    public partial class Main : Form
    {
        private string imgName = string.Empty;
        private string imgPath = string.Empty;
        public Main()
        {
            InitializeComponent();
        }

        private void btnGetImgMsg_Click(object sender, EventArgs e)
        {
            string[] files = System.IO.Directory.GetFiles(Config.PicturePath);
           // MessageBox.Show(Path.GetFileName(files[0]));
            //tBoxPicName.Text = Path.GetFileName(files[0]);
            imgName = tBoxPicName.Text = Path.GetFileName(files[0]);
            imgPath = string.Format("{0}\\{1}", Config.PicturePath, imgName);
            Image img = Image.FromFile(imgPath);
            string imgMsg = string.Format("图片高度:{0}px,图片宽度:{1}", img.Height, img.Width);
            labShowImgMsg.Text = imgMsg;

            tBX.Text = "0";
            tBY.Text = "0";
            tBW.Text = img.Width.ToString();
            tBH.Text = img.Height.ToString();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            labSorceImgPath.Text = Config.PicturePath;
            tBoutPut.Text = Config.OutPutPath;
        }

        private void btnCutImg_Click(object sender, EventArgs e)
        {
          
                int x, y, w, h;
                int.TryParse(tBX.Text, out x);
                int.TryParse(tBY.Text, out y);
                int.TryParse(tBW.Text, out w);
                int.TryParse(tBH.Text, out h);
          
                CutImgKit(x,y,w,h);
        }

        private void CutImgKit(int x,int y,int w,int h)
        {
            Bitmap bmpBase = new Bitmap(imgPath);//构造图片对象
           // Image img = Image.FromFile(imgPath);
            int count = (bmpBase.Height - y) / h;//能切几块
            int lastH = (bmpBase.Height - y) % h;//最后一块的高度，如果整除就是0
            if (lastH > 0)
            {
                count++; //要多加一个
            }
            try{
                string newFilePath=string.Empty;
                Bitmap bmpNew=null;
                for(int i=0;i<count;i++){
                    Rectangle rect = new Rectangle(x, y+h*i, w, h);//划定一个矩形区域  只变高度，即y
                    if (i == count - 1 && lastH>0)
                    {
                        rect = new Rectangle(x, y + h * i, w, lastH);
                    }                  
                   bmpNew = bmpBase.Clone(rect, bmpBase.PixelFormat);//将这个区域克隆成新的图片
                   newFilePath = string.Format("{0}\\{1}", tBoutPut.Text, i+".jpg");
                   bmpNew.Save(newFilePath, ImageFormat.Jpeg);//保存
                }

                bmpBase.Dispose();// 释放资源
                bmpNew.Dispose();//释放资源
                 
            }catch(Exception){
                MessageBox.Show("截图失败");
                return;
            }

            MessageBox.Show("截图成功");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnCutImgOne_Click(object sender, EventArgs e)
        {
            int x, y, w, h;
            int.TryParse(tBX.Text, out x);
            int.TryParse(tBY.Text, out y);
            int.TryParse(tBW.Text, out w);
            int.TryParse(tBH.Text, out h);
            try
            {
                Bitmap bmpBase = new Bitmap(imgPath);//构造图片对象
                Rectangle rect = new Rectangle(x, y, w, h);
                Bitmap bmpNew = bmpBase.Clone(rect, bmpBase.PixelFormat);//将这个区域克隆成新的图片
                string newFilePath = tBoutPut.Text;// string.Format("{0}\\{1}", tBoutPut.Text, "1.jpg");
                bmpNew.Save(newFilePath, ImageFormat.Jpeg);//保存
                bmpBase.Dispose();// 释放资源
                bmpNew.Dispose();//释放资源
            }
            catch (Exception)
            {
                MessageBox.Show("截图失败");
                return;
            }

            MessageBox.Show("截图成功");
        }
       
    }
}
